package com.example.demo.service;

import com.example.demo.bean.Records;
import com.example.demo.mapper.*;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class MessageCostService {
    @Resource
    private DistanceMapper distanceMapper;
    @Resource
    private Message_OwnerMapper messageOwnerMapper;
    @Resource
    private MessageMapper messageMapper;
    @Resource
    private CarMapper carMapper;
    @Resource
    private RecordMapper recordMapper;
    @Resource
    private OwnersMapper ownersMapper;

//    public double getEmpty_distance(int message_id){
//        List<Map<String,Object>> start_end = messageOwnerMapper.getEmptyDistance(message_id);
//        double distance = 0;
//        for (Map<String,Object> row : start_end){
//            int start = Integer.parseInt(row.get("poi_id").toString());
//            int end = Integer.parseInt(row.get("destination").toString());
//            distance = distance + distanceMapper.getDistance(start,end);
//        }
//        return distance;
//    }
//
//    public double findCost(int message_id){
//        return messageMapper.findCost(message_id);
//    }
//
//    public double car_utilization(int message_id){
//        return (double) messageOwnerMapper.getCar_num(message_id)/(double) carMapper.selectCar_num();
//    }
//
//    public double getLoad_waste(int message_id){
//        List<Map<String, Object>> lists = messageOwnerMapper.get_many(message_id);
//        double weight = Double.parseDouble(lists.get(0).get("goods_weight").toString());
//        for(int i = 0;i<lists.size()-1;i++){
//            weight = weight - Double.parseDouble(lists.get(i).get("load_m").toString());
//        }
//        return 1-weight/Double.parseDouble(lists.get(lists.size()-1).get("load_m").toString());
//    }

    public Map<String,Object> get_cost_list(int page){
        Map<String,Object> list_message = new HashMap<>();
        int record_num = recordMapper.get_max_page();
        if(record_num % 5 == 0)
            list_message.put("maxPage",record_num / 5);
        else
            list_message.put("maxPage",record_num / 5 + 1);
        List<Records> costs = recordMapper.get_costs((page-1)*5+1,Math.min(page*5,record_num));
        for(Records records : costs){
            records.setRecordTime(records.getRecord_time().toLocalDateTime().toLocalDate());
        }
        list_message.put("costs",costs);
        return list_message;
    }

    public List<Map<String,Object>> get_cost_message(int record_id){
        List<Map<String,Object>> messages_message = messageMapper.get_message_message(record_id);
        int cars_num = carMapper.selectCar_num();
        for(Map<String,Object> message_message : messages_message){
            int car_num = ownersMapper.get_used_car(Integer.parseInt(message_message.get("message_id").toString()));
            message_message.put("car_utilization",car_num/(double) cars_num);
            message_message.put("wait_time",ownersMapper.get_wait_time(Integer.parseInt(message_message.get("message_id").toString())));
        }
        return messages_message;
    }

}
